<template>
  <!-- 登录页面 -->
  <div class="login-page">
    <van-nav-bar
      class="navBar-parts"
      title="登录页面"
    >
       <div slot="left" class="returnUpper" @click="$router.push('/my')">
        <van-icon name="arrow-left" color="#fff"/>
        <span class="returnBtn">返回</span>
       </div>
       <div slot="right" class="returnUpper" @click="$router.push('/register')">
        <span class="returnBtn">注册账号</span>
       </div>
    </van-nav-bar>

    <van-form @submit="onSubmit">
      <van-field
        v-model="user.mobile"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="formUserRules.mobile"
      />
      <van-field
        v-model="user.code"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="formUserRules.code"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit" color="#ed3651">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import {postUserSignIn} from '@/service/getDate'
export default {
  name: 'LoginPage',
  data() {
    return {
      user:{
        mobile: '',
        code: '',
      },
      formUserRules: {
        mobile: [
          {
            required: true, // 手机号码不能为空

            message: '手机号码不能为空'
          },
          {
            pattern: /^1[3|4|5|6|7|8|9]\d{9}$/, // 正则规则
            message: '请输入正确的手机号码格式'
          }
        ],
        code: [
          {
            required: true, // 必须输入
            message: '密码不能为空'
          },
          {
            pattern: /^\d{6}$/,
            message: '请输入6位数字的密码'
          }
        ]
      }
    };
  },
  
  methods: {
    async onSubmit() {
      try {
        const {data} = await postUserSignIn(this.user)
        this.$store.commit('TOKEN_KEY', data.data)
        this.$toast.success('登录成功')
        this.$router.push('/')
      } catch (err) {
        console.log(err);
        if(err.response.status == 400) {
          this.$toast.fail('登录失败，请检查账号或密码是否正确')
        } else {
          this.$toast.fail('登录失败')
        }
      }
    },
  }
 }
</script>

<style lang="scss" scoped>
.login-page {
  .navBar-parts {
    .returnUpper {
      color: #fff;
      font-size: 30px;
    }
  }

}

</style>